%% Copyright (C) 2007 Vijay Gajula <gajulak@gmail.com>.
%% All rights reserved.
%%
%% Redistribution and use in source and binary forms, with or without
%% modification, are permitted provided that the following conditions
%% are met:
%%
%% 1. Redistributions of source code must retain the above copyright
%%    notice, this list of conditions and the following disclaimer.
%% 2. Redistributions in binary form must reproduce the above
%%    copyright notice, this list of conditions and the following
%%    disclaimer in the documentation and/or other materials provided
%%    with the distribution.
%%
%% THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
%% OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
%% WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
%% ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
%% DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
%% DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
%% GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
%% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
%% WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
%% NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
%% SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

-module(mod_users).
-vsn(1.0).
-author('gajulak@gmail.com').
-export([process_get/1, process_edit/1, sys_users/1]).
-export([users_info/1]).

-include("xmerl.hrl").
-include("netconf.hrl").

-define(TREE, { name,
				[ passwd,
				  passwd_expiry,
			  	  admin,
			  	  nonadmin,
				  certificate,
			  	  ipsecvpn,
			  	  sslvpn,
			  	  pptpvpn,
				  bandwidth,              %% Data Rate
				  access_start_time,			  %% Access Restrictions
				  access_end_time,			  %% Access Restrictions
				  access_days,
		  	  	  status
				  %% login_start_time
				  %% url_white_list
				  %% url_black_list
				  %% urls_visted
				  %% bandwidth_usage
				  %% chat_id          %% yahoo, gmail, 
				  %% used_protocols  service(ftp/telnet...), time, data_transfered
				  %% virus_found
				  %% spam_sent 		 %% emails sent
				  %% open_ports      %% shows current open ports
				  %% mac_address
				  %% ip_address
				  %% hostname
				  %% location,        %% build location
				  %% cube,			   %% cube or room number
				  %% port,			   %% socket number for ethernet connection.
				  %% login_end_time,
			  ]}).

process_get(E) ->
	%% io:format("mod_users:~p~n",[E]),
	sys_users(E).

sys_users([#xmlElement{name = all}=E|T]) 
   when record(E, xmlElement) ->
	users_info(all);
	
sys_users([H|T]) -> sys_users(T);
sys_users([]) -> ok.

users_info(all) ->
	Users = db:get_users(),
	Xml = generate_xml(Users, []),
	{ok, Xml}.

generate_xml([], Xml) -> Xml;
generate_xml([H|T], Xml) ->
     Str = io_lib:format("
			<users>
			<name>~s</name>
			<passwd_expiry>~s</passwd_expiry>
			<admin>~p</admin>
			<nonadmin>~p</nonadmin>
			<certificate>~p<certificate>
			<ipsecvpn>~p</ipsecvpn>
			<sslvpn>~p</sslvpn>
			<pptpvpn>~p</pptpvpn>
			<bandwidth>~p</bandwidth>
			<access_start_time>~p</access_start_time>
			<access_end_time>~p</access_end_time>
			<access_days>~p</access_days>
			<status>~p</status>
			</users>~n", 
			[H#users.name, 
			 H#users.passwd_expiry,
			 H#users.admin,
			 H#users.nonadmin,
			 H#users.certificate,
			 H#users.ipsecvpn,
			 H#users.sslvpn,
			 H#users.pptpvpn,
			 H#users.bandwidth,              %% Data Rate
			 H#users.access_start_time,			  %% Access Restrictions
			 H#users.access_end_time,			  %% Access Restrictions
			 H#users.access_days,
			 H#users.status
			 ]),
      generate_xml(T, Xml++Str).


process_edit(E) ->
	%% io:format("mod_users:~p~n",[E]),
	R = top_node(?TREE, E, #users{}),
	%% io:format("End process_edit:~p~n",[R]),
	R.

top_node({Node, SubNode}, [#xmlElement{name = Node}=E|T], If) 
   when record(E, xmlElement) ->
	%% io:format("11 ~p~n",[E]),
	Val = get_text(E#xmlElement.content),
	%% io:format("Name:~p~n",[Val]),
	case db:get_users(Val) of 
	     [] -> 	
	     		Str = io_lib:format("<error/>",[]),
	             	{ok, Str};
		[If1] ->  %% io:format("DB If : ~p~n", [If1]), 
			sub_node(SubNode, T, If1)
	end;

top_node(Tree, [H|T], If) when record(H, xmlElement) ->
	%% io:format("22, E~p~n",[H]),
	top_node(Tree, T, If);

top_node(Tree, [H|T], If) when record(H, xmlText) ->
	%% io:format("331  ~p~n",[H]),
	top_node(Tree, T, If);

top_node(_, [], If) ->
	%% io:format("INTERFACE SET11:~p~n", [If]),
	Str = io_lib:format("<ok/>",[]),
	{ok, Str};

top_node(Node, _E, _If) ->
	io:format("Error:~p~n",[_E]),
	Str = io_lib:format("<error/>",[]),
	{ok, Str}.

sub_node(SNodes, [E|T], If) ->
	%% io:format("4444  ~p~n",[E]),
	If2 = process_attributes(SNodes, E, If),
	%% io:format("55  ~p~n",[T]),
	sub_node(SNodes, T, If2);

sub_node(_, [], If) ->
	%% io:format("INTERFACE SET22:~p~n", [If]),
	db:set_record(If),
	Str = io_lib:format("<ok/>",[]),
	{ok, Str}.

process_attribute(passwd_expiry, #xmlElement{name = passwd_expiry}=E, If) 
   when record(E, xmlElement) ->
	Val = get_text(E#xmlElement.content),
	{true, If#users{ passwd_expiry = Val }};

process_attribute(admin, #xmlElement{name = admin}=E, If) 
   when record(E, xmlElement) ->
	Val = get_text(E#xmlElement.content),
	{true, If#users{ admin = Val }};

process_attribute(nonadmin, #xmlElement{name = nonadmin}=E, If) 
   when record(E, xmlElement) ->
	Val = get_text(E#xmlElement.content),
	{true, If#users{ nonadmin = Val }};

process_attribute(certificate, #xmlElement{name = certificate}=E, If) 
   when record(E, xmlElement) ->
	Val = get_text(E#xmlElement.content),
	{true, If#users{ certificate = Val }};

process_attribute(ipsecvpn, #xmlElement{name = ipsecvpn}=E, If) 
   when record(E, xmlElement) ->
	Val = get_text(E#xmlElement.content),
	{true, If#users{ ipsecvpn = Val }};

process_attribute(sslvpn, #xmlElement{name = sslvpn}=E, If) 
   when record(E, xmlElement) ->
	Val = get_text(E#xmlElement.content),
	{true, If#users{ sslvpn = Val }};

process_attribute(pptpvpn, #xmlElement{name = pptpvpn}=E, If) 
   when record(E, xmlElement) ->
	Val = get_text(E#xmlElement.content),
	{true, If#users{ pptpvpn = Val }};

process_attribute(bandwidth, #xmlElement{name = bandwidth}=E, If) 
   when record(E, xmlElement) ->
	Val = get_text(E#xmlElement.content),
	{true, If#users{ bandwidth = Val }};

process_attribute(access_start_time, #xmlElement{name = access_start_time}=E, If) 
   when record(E, xmlElement) ->
	Val = get_text(E#xmlElement.content),
	{true, If#users{ access_start_time = Val }};

process_attribute(access_end_time, #xmlElement{name = access_end_time}=E, If) 
   when record(E, xmlElement) ->
	Val = get_text(E#xmlElement.content),
	{true, If#users{ access_end_time = Val }};

process_attribute(access_days, #xmlElement{name = access_days}=E, If) 
   when record(E, xmlElement) ->
	Val = get_text(E#xmlElement.content),
	{true, If#users{ access_days = Val }};

process_attribute(status, #xmlElement{name = status}=E, If) 
   when record(E, xmlElement) ->
	%% io:format("111  ~p~n",[E]),
	Val = get_text(E#xmlElement.content),
	If1 = If#users{ status = Val },
	{true, If1};
	
process_attribute(_, _, If) -> 
	%% io:format("122  ~p~n",[If]),
	{false, If}.
	
process_attributes([H|T], E, If) when record(E, xmlElement) ->
	case process_attribute(H, E, If) of
	   {true, If1} -> If1;
	   {false, _If2} -> process_attributes(T, E, If)
	end;
process_attributes(_, _, If) ->
	%% io:format("133  ~p~n",[If]),
	If.

get_text([X|_]) when record(X, xmlText) ->  X#xmlText.value;
get_text(_) ->  [].
